﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XinYuan.Office.Models;
using XinYuanOfficeWinForm.UserInfoServiceReference;
using System.Threading;

namespace XinYuanOfficeWinForm
{
    public partial class FrmLogin : Form
    {
        private delegate void loginDelegate();
        FrmLoad f1 = null;
        bool flag = false;
        string msg = string.Empty;
        Operator userInfo;
        public FrmLogin()
        {
            InitializeComponent();
            work.DoWork += new DoWorkEventHandler(work_DoWork);
            work.RunWorkerCompleted += new RunWorkerCompletedEventHandler(work_RunWorkerCompleted);
        }
        private void DoWork()
        {
            loginDelegate login = new loginDelegate(LoginIn);
            this.BeginInvoke(login);
        }
        void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (flag == true)
            {
                User.Id = userInfo.Id;
                User.UserId = userInfo.OperatorName;
                Admin.FrmMain frmMain = new Admin.FrmMain(this);
                frmMain.WindowState = FormWindowState.Maximized;
                this.Hide();
                frmMain.Show();
            }
            else
            {
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        void work_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(10000);
            DoWork();
        }

        private void LoginIn()
        {
            try
            {
                OperatorServiceReference.OperatorServiceClient client = new OperatorServiceReference.OperatorServiceClient();
                
                userInfo = new Operator();
                flag = client.LoginIn(txtAccount.Text.Trim(), txtPassword.Text.Trim(), ref userInfo, ref msg);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtAccount, "");
            errorProvider1.SetError(txtPassword, "");
            if (txtAccount.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtAccount, "用户名不能为空");
                txtAccount.Focus();
                return;
            }
            if (txtPassword.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtPassword, "密码不能为空");
                txtPassword.Focus();
                return;
            }
            work.RunWorkerAsync();
            f1 = new FrmLoad(this.work);
            f1.StartPosition = FormStartPosition.CenterScreen;
            f1.ShowDialog(this);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            Admin.Permission.FrmRights frmRight = new Admin.Permission.FrmRights();
            frmRight.ShowDialog();
        }

        private void picMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
